package com.heoller.service.generator.postprocess;

import com.heoller.constant.ModuleConstants;
import com.heoller.dto.CommonParam;
import com.heoller.dto.CustomParam;
import com.heoller.dto.WriteParam;
import com.heoller.service.template.TemplateService;
import freemarker.template.Template;
import java.util.List;
import java.util.zip.ZipOutputStream;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author huliang
 * @date 2021-11-23 15:41
 */
public abstract class AbstractGeneratorPostProcessService implements BeanNameAware {

    private String beanName;

    @Autowired
    protected TemplateService templateService;

    /**
     * 具体模块后续处理
     * @param zip
     * @param module
     * @param customParam
     */
    public abstract void doPostProcess(ZipOutputStream zip, String module, CustomParam customParam);

    public String buildJavaPath(String module, CustomParam customParam) {
        String modulePath = buildModulePath(module, customParam);
        return modulePath + "/src/main/java/" + customParam.getGroupId().replaceAll("\\.", "/") + "/" + module;
    }

    public String buildResourcesPath(String module, CustomParam customParam) {
        String modulePath = buildModulePath(module, customParam);
        return modulePath + "/src/main/resources";
    }

    public String buildModulePath(String module, CustomParam customParam) {
        return customParam.getArtifactId() + "/" + customParam.getArtifactId() + "-" + module;
    }

    public void writePackageInfo(List<String> pathList, String module, CustomParam customParam,
        String javaPath, ZipOutputStream zip) {
        pathList.forEach(relativePath -> templateService.writeDirectory(zip, javaPath + relativePath + "/"));
        if (module.equals(ModuleConstants.INTERFACES)) {
            return;
        }
        writeTestPackageInfo(module, customParam, zip);
    }

    private void writeTestPackageInfo(String module, CustomParam customParam, ZipOutputStream zip) {
        String modulePath = buildModulePath(module, customParam);
        String fileName = modulePath + "/src/test/java/package-info.java";
        Template template = templateService.getTemplate(module, "test-package-info.java.ftl");
        templateService.writeFile(zip, fileName, template, customParam);
    }

    CommonParam writeBatch(CustomParam customParam, String module, List<WriteParam> writeParams,
        String javaPath, ZipOutputStream zip) {
        CommonParam commonParam = new CommonParam(customParam.getGroupId(), customParam.getArtifactId(), module,
            Strings.EMPTY);
        for (WriteParam writeParam : writeParams) {
            Template template = templateService.getTemplate(module, writeParam.getTemplateName());
            templateService.writeFile(zip, javaPath + writeParam.getRelativePath() +
                writeParam.getJavaFileSuffixName(), template, commonParam);
        }
        return commonParam;
    }

    @Override
    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public String getBeanName() {
        return beanName;
    }
}
